package com.neeraj.SharingArtifacts;

import java.io.File;
import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class AllArtifactsTab extends ListActivity {
	
	private static final String DEBUG_TAG = "ALL TABS";
	private static final int MEDIA_TYPE_TEXT = 4;
	private static AllArtifactsTab instance;
	private ListView listviewAll;		
	private ArrayList<String> child;
	DatabaseHelper dbHelper;
	ArrayList<String> checkedAllItems;
	ArrayAdapter<String> fileList;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_artifact_layout);
        instance = this;
        listviewAll = (ListView)findViewById(android.R.id.list);
        dbHelper=new DatabaseHelper(this);
		dbHelper.insertTypeData();
        showAllFiles();
    }
    
    public static AllArtifactsTab getInstance() {
    	return instance;
    }
    
    void showAllFiles(){
    	File dir = new File(Environment.getExternalStoragePublicDirectory(
	              Environment.DIRECTORY_DCIM), "SharingArtifacts");
    	File[] subDir = dir.listFiles();

    	child = new ArrayList<String>();
    	checkedAllItems = new ArrayList<String>();
//    	String[] children = dir.list(); 
    	if (subDir == null) {
    	    // Either dir does not exist or is not a directory
    	} else {
    	    for (int i=0; i<subDir.length; i++) {
    	        // Get filename of file or directory
    	    	File file = subDir[i];
    	    	if(file.isDirectory()){
    	    		String[] children = file.list();
    	    		for(int j=0;j<children.length;++j){
    	    			child.add(children[j]);
    	    		}
    	    	}    	    	    	        
    	    }
    	}
    	child.addAll(dbHelper.getTextIDs(MEDIA_TYPE_TEXT));
//    	Collections.copy(child, dbHelper.getTextIDs(MEDIA_TYPE_TEXT));
    	Log.d(DEBUG_TAG, child.toString());
    	
    	fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, child);
    	listviewAll.setAdapter(fileList);
    	listviewAll = getListView();
    	listviewAll.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    	Toast.makeText(this, "List choice mode: " + listviewAll.getChoiceMode(),
				Toast.LENGTH_SHORT).show();
    	
    	listviewAll.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String item = (String) listviewAll.getAdapter().getItem(arg2);
				CheckedTextView listItem = (CheckedTextView)arg1;
				if(!listItem.isChecked()){
					System.out.println(item + ":Status=" + dbHelper.isUploaded(item));
					if(!dbHelper.isUploaded(item)){
						Log.d(DEBUG_TAG, "Checked!!");
						checkedAllItems.add(item);
					}						
				}
				else
					Log.d(DEBUG_TAG, "Not Checked!!");
				Log.d(DEBUG_TAG, item);				
			}
		});
    	
    }
  
    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	super.onBackPressed();
    	Toast.makeText(this, checkedAllItems.toString(), Toast.LENGTH_LONG).show();
    }
}
